package 算法.LeetCode91.每日打卡题目;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author: 谢君臣
 * @create: 2021-02-23 13:07
 * @founction: XXXX
 */
public class day23 {
    public static void main(String[] args) {
        findSubstring("wordgoodgoodgoodbestword", new String[]{"good", "good", "word", "word"});
    }

    public static List<Integer> findSubstring(String s, String[] words) {
        List<Integer> list = new ArrayList<>();
        if (s.length() < 1 || words.length < 1) {
            return list;
        }
        HashMap<String, Integer> hashSet = new HashMap<>();
        for (String word : words)
            hashSet.put(word, hashSet.getOrDefault(word, 0) + 1);
        int l = words[0].length();
        int i = 0;
        while (i + l * words.length <= s.length()) {
            // 判断 i 到 i+ l*words.length 中 每l个字符串组成的次的哈希表 是否相等
            HashMap<String, Integer> temp = new HashMap<>();
            for (int j = 0; j < words.length; j++) {
                String str = s.substring(i + j * l, i + l + j * l);
                if (hashSet.containsKey(str))
                    temp.put(str, temp.getOrDefault(str, 0) + 1);
                else
                    break;
            }
            if (temp.equals(hashSet))
                list.add(i);
            i++;
        }
        return list;
    }
}
